package com.mrd.gtimpframe.action;

import com.mrd.base.Jqgrid;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimpframe.entity.DropdownResult;
import com.mrd.gtimpframe.entity.Notice;
import com.mrd.gtimpframe.service.NoticeService;
import com.mrd.gtimpframe.service.impl.NoticeServiceImpl2;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by dusy on 2017/11/29.
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Qualifier("noticeServiceImpl")
    @Autowired
    private NoticeService noticeService;

    @Autowired
    @Qualifier("noticeServiceImpl2")
    private NoticeService noticeService2;

    /**
     * 公告首页
     */
    @RequestMapping("/noticeManager")
    public String noticeManager() {
        return "/gtimpframe/notice/notice";
    }

    /**
     * 获取适用角色
     */
    @RequestMapping("/appliedRoles")
    @ResponseBody
    public DropdownResult getAppliedRoles() {
        return new DropdownResult(noticeService.getAppliedRoles());
    }

    /**
     * 发布公告
     */
    @RequestMapping("/publish")
    @ResponseBody
    public Result noticePublish(String noticeId) {
        if (StringUtils.isEmpty(noticeId)) {
            return Result.newFailInstance("请选择需要发布的公告");
        }
        return noticeService.noticePublish(noticeId);
    }

    /**
     * 图片上传及更换
     *
     * @param noticeId       公告id
     * @param noticeType     公告类型 new:新增
     * @param replacePicture 需要替换掉的图片名称
     * @param file           上传的图片
     */
    @RequestMapping("/uploadOrReplace")
    @ResponseBody
    public Result uploadOrReplace(String noticeId, String noticeType, String replacePicture, MultipartFile file) {
        return noticeService.uploadOrReplace(noticeId, noticeType, replacePicture, file);
    }

    /**
     * 读取图片
     *
     * @param temp     非空表示从临时文件夹获取图片
     * @param noticeId 公告id
     * @param fileName 要读取的图片名
     */
    @RequestMapping("/picture/{noticeId}")
    public void getPicture(HttpServletResponse response, @PathVariable("noticeId") String noticeId, String fileName, String temp) {
        noticeService.getPicture(response, temp, noticeId, fileName);
    }

    /**
     * 获取公告列表
     *
     * @param edit 非空表示编辑页面获取
     */
    @RequestMapping("/noticeList")
    @ResponseBody
    public void getNoticeList(HttpServletResponse response, Jqgrid jqgrid, String edit) {
        noticeService2.getNoticeList(response, jqgrid, edit);
    }

    /**
     * 删除公告
     */
    @RequestMapping("/deleteNotice")
    @ResponseBody
    public Result deleteNotice(String noticeId) {
        if (noticeId == null) {
            return Result.newFailInstance("请选择需要删除的公告");
        } else {
            Notice notice = new Notice();
            notice.setId(noticeId);
            notice.setState("2");//进行逻辑删除
            notice.setModifyTime(new Date());
            notice.setModifier(LoginUtil.getLoginUserId());
            noticeService.updateByPrimaryKeySelective(notice);
            return Result.newSuccessInstance("已删除");
        }
    }

    /**
     * 展示最新公告
     */
    @RequestMapping("showNewNotice")
    @ResponseBody
    public Result showNewNotice() {
        return noticeService.showNewNotice();
    }

    /**
     * 标记用户已看了公告
     *
     * @param noticeIds 多个公告id用逗号拼接
     */
    @RequestMapping("/haveReceivedNotice")
    @ResponseBody
    public Result haveReceivedNotice(String noticeIds) {
        return noticeService.haveReceivedNotice(noticeIds);
    }

    /**
     * 打开编辑页面时的预备处理
     */
    @RequestMapping("/dealBeforeEdit")
    @ResponseBody
    public Result dealBeforeEdit(String noticeId) {
        return noticeService.dealBeforeEdit(noticeId);
    }

    /**
     * 提交公告
     */
    @RequestMapping("/submit")
    @ResponseBody
    public Result submit(Notice notice) {
        return noticeService.submit(notice);
    }

    /**
     * 获取公告信息
     *
     * @param noticeId 公告id
     */
    @RequestMapping("/getNotice")
    @ResponseBody
    public Result getNotice(String noticeId) {
        if (noticeId == null) {
            return Result.newFailInstance("参数错误");
        }
        Notice notice = noticeService.selectByPrimaryKey(noticeId);
        if (notice == null) {
            return Result.newFailInstance("该公告不存在");
        } else {
            return Result.newSuccessInstance("获取公告", notice);
        }
    }

    /**
     * 删除图片
     *
     * @param noticeId    公告id
     * @param pictureName 图片名
     */
    @RequestMapping("/deletePicture")
    @ResponseBody
    public Result deletePicture(String noticeId, String pictureName) {
        return noticeService.deletePicture(noticeId, pictureName);
    }
}
